<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>LVS结合keepalived配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>LVS结合keepalived配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a> <a class="article__tag-link" href="/tags/keepalived/">keepalived</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为 NAT、DR 已经配置过一些操作，而使用 keepalived 操作和以前的操作是有些冲突的，所以先做一些处理。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 dr 上执行 ：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@dir ~]<span class="comment"># ipvsadm -C</span></div><div class="line">[root@dir ~]<span class="comment"># ifconfig eth0:0 down</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为什么要引入 keepalived ，之前的 lvs 虽然已经配置成功也实现了负载均衡，但是测试的时候发现，当某台 real server 把 nginx 停掉，那么 director 照样会把请求转发过去，这样就造成了某些请求不正常。所以需要有一种机制用来检测 real server 的状态，也就是 keepalived 。它的作用除了可以检测 RS 状态外，还可以检测备用 director 的状态，也就是说 keepalived 可以实现 HA 集群的功能，当然也需要一台备用 director 服务器。备用 director 也需要安装一下 keepalived 软件。两台 director 上都执行命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y keepalived</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主 director 上安装好后，编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@dir ~]<span class="comment"># vim /etc/keepalived/keepalived.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入或更改如下配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">vrrp_instance VI_1 &#123;</div><div class="line">    state MASTER <span class="comment">#备用服务器上为 BACKUP</span></div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 51</div><div class="line">    priority 100 <span class="comment">#备用服务器上为90</span></div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 1111</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.0.64</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">virtual_server 192.168.0.64 80 &#123;</div><div class="line">    delay_loop 6 <span class="comment">#(每隔10秒查询realserver状态)</span></div><div class="line">    lb_algo wlc <span class="comment">#(lvs 算法)</span></div><div class="line">    lb_kind DR <span class="comment">#(Direct Route)</span></div><div class="line">    persistence_timeout 60 <span class="comment">#(同一IP的连接60秒内被分配到同一台realserver)</span></div><div class="line">    protocol TCP <span class="comment">#(用TCP协议检查realserver状态)</span></div><div class="line"></div><div class="line">    real_server 192.168.0.66 80 &#123;</div><div class="line">        weight 100 <span class="comment">#(权重)</span></div><div class="line">        TCP_CHECK &#123;</div><div class="line">        connect_timeout 10 <span class="comment">#(10秒无响应超时)</span></div><div class="line">        nb_get_retry 3</div><div class="line">        delay_before_retry 3</div><div class="line">        connect_port 80</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    real_server 192.168.0.65 80 &#123;</div><div class="line">        weight 100</div><div class="line">        TCP_CHECK &#123;</div><div class="line">        connect_timeout 10</div><div class="line">        nb_get_retry 3</div><div class="line">        delay_before_retry 3</div><div class="line">        connect_port 80</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上为主 director 的配置文件，从 director 的配置文件只需要修改如下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">state MASTER -&gt; state BACKUP</div><div class="line">priority 100 -&gt; priority 90</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改后从配置为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">vrrp_instance VI_1 &#123;</div><div class="line">    state BACKUP <span class="comment">#主服务器上为 MASTER</span></div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 51</div><div class="line">    priority 90 <span class="comment">#主服务器上为100</span></div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 1111</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.0.64</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">virtual_server 192.168.0.64 80 &#123;</div><div class="line">    delay_loop 6 <span class="comment">#(每隔10秒查询realserver状态)</span></div><div class="line">    lb_algo wlc <span class="comment">#(lvs 算法)</span></div><div class="line">    lb_kind DR <span class="comment">#(Direct Route)</span></div><div class="line">    persistence_timeout 60 <span class="comment">#(同一IP的连接60秒内被分配到同一台realserver)</span></div><div class="line">    protocol TCP <span class="comment">#(用TCP协议检查realserver状态)</span></div><div class="line"></div><div class="line">    real_server 192.168.0.66 80 &#123;</div><div class="line">        weight 100 <span class="comment">#(权重)</span></div><div class="line">        TCP_CHECK &#123;</div><div class="line">        connect_timeout 10 <span class="comment">#(10秒无响应超时)</span></div><div class="line">        nb_get_retry 3</div><div class="line">        delay_before_retry 3</div><div class="line">        connect_port 80</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    real_server 192.168.0.65 80 &#123;</div><div class="line">        weight 100</div><div class="line">        TCP_CHECK &#123;</div><div class="line">        connect_timeout 10</div><div class="line">        nb_get_retry 3</div><div class="line">        delay_before_retry 3</div><div class="line">        connect_port 80</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置完成 keepalived 后，需要开启端口转发（主从都要做）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> 1 &gt; /proc/sys/net/ipv4/ip_forward</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，两个 rs 上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sh /usr/<span class="built_in">local</span>/sbin/lvs_dr_rs.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，两个 director 上启动 keepalived 服务（先主后从）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/keepalived start</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/19. LVS结合keepalived配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
